/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.mobile2you.nci.android.data.InitialDataLoader;
import com.mobile2you.nci.android.data.SqlHelper;
import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.domain.Annuaires;
import com.mobile2you.nci.android.utils.ListAnnuaireAdapter;
import com.mobile2you.nci.android.utils.LoadImagePool;

/**
 * @author JBromo
 * 
 */
public class MenuAnnuaireDetails extends Activity {

	ViewFlipper flipper;
	ListView expandableListView;

	List<Annuaires> list = new ArrayList<Annuaires>();

	ListAnnuaireAdapter annuaireAdapter;

	TextView title;

	private class LoadLocalData extends
			AsyncTask<String, String, List<Annuaires>> {

		@Override
		protected List<Annuaires> doInBackground(String... params) {
			List<Annuaires> list = SqlHelper.getInstance(
					MenuAnnuaireDetails.this).getLocalAnnuaire();
			if (list == null || list.isEmpty()) {
				list = InitialDataLoader.getAnnuaire(MenuAnnuaireDetails.this);
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Annuaires> result) {
			super.onPostExecute(result);
			list = result;
			showData(result, ListAnnuaireAdapter.TYPE_ABS);
			new SendContactTask().execute();

		}

	}

	private class SendContactTask extends
			AsyncTask<String, String, List<Annuaires>> {

		@Override
		protected List<Annuaires> doInBackground(String... params) {
			String[] logins = SqlHelper.getInstance(MenuAnnuaireDetails.this)
					.getLocalLogin();
			return WebServicesRequest.getListAnnuaires(
					MenuAnnuaireDetails.this, logins[0], logins[1]);
		}

		@Override
		protected void onPostExecute(List<Annuaires> result) {
			super.onPostExecute(result);
			if (result != null && !result.isEmpty()) {
				showData(result, ListAnnuaireAdapter.TYPE_ABS);
			}
		}

	}

	private void initView() {
		final ImageView abc = (ImageView) findViewById(R.id.view_search);
		final ImageView activity = (ImageView) findViewById(R.id.view_favoris);
		final ImageView centre = (ImageView) findViewById(R.id.view_country);
		abc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showData(list, ListAnnuaireAdapter.TYPE_ABS);
				setupIcon(ListAnnuaireAdapter.TYPE_ABS, abc, activity, centre);
			}
		});

		activity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showData(list, ListAnnuaireAdapter.TYPE_ACTIVITY);
				setupIcon(ListAnnuaireAdapter.TYPE_ACTIVITY, abc, activity,
						centre);
			}
		});

		centre.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showData(list, ListAnnuaireAdapter.TYPE_CENTRE);
				setupIcon(ListAnnuaireAdapter.TYPE_CENTRE, abc, activity,
						centre);
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_annuaire_detail);
		initView();
		flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		title = (TextView) findViewById(R.id.textView1);
		new LoadLocalData().execute();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// menu.add(Menu.NONE, ListAnnuaireAdapter.TYPE_ABS, 1, "Alphabet")
		// .setIcon(R.drawable.sort_abc_gris);
		// menu.add(Menu.NONE, ListAnnuaireAdapter.TYPE_ACTIVITY, 2, "Activity")
		// .setIcon(R.drawable.activity);
		// menu.add(Menu.NONE, ListAnnuaireAdapter.TYPE_CENTRE, 3, "Centre")
		// .setIcon(R.drawable.ic_centre);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (flipper.getDisplayedChild() == 1) {
				title.setText(" Annuaires ");
				flipper.setDisplayedChild(0);
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getItemId() == ListAnnuaireAdapter.TYPE_ABS) {
			showData(list, ListAnnuaireAdapter.TYPE_ABS);
		}
		if (item.getItemId() == ListAnnuaireAdapter.TYPE_CENTRE) {
			showData(list, ListAnnuaireAdapter.TYPE_CENTRE);
		}
		if (item.getItemId() == ListAnnuaireAdapter.TYPE_ACTIVITY) {
			showData(list, ListAnnuaireAdapter.TYPE_ACTIVITY);
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

	private void setupIcon(int type, ImageView abc, ImageView activity,
			ImageView centre) {

		switch (type) {
		case ListAnnuaireAdapter.TYPE_ABS:
			abc.setImageResource(R.drawable.sort_abc);
			centre.setImageResource(R.drawable.sort_centre_gris);
			activity.setImageResource(R.drawable.sort_activity_gris);

			break;
		case ListAnnuaireAdapter.TYPE_ACTIVITY:

			abc.setImageResource(R.drawable.sort_abc_gris);
			centre.setImageResource(R.drawable.sort_centre_gris);
			activity.setImageResource(R.drawable.sort_activity_click);

			break;

		case ListAnnuaireAdapter.TYPE_CENTRE:

			abc.setImageResource(R.drawable.sort_abc_gris);
			centre.setImageResource(R.drawable.sort_centre_click);
			activity.setImageResource(R.drawable.sort_activity_gris);

			break;

		}
	}

	private void showData(List<Annuaires> result, int type) {

		expandableListView = (ListView) findViewById(R.id.expandableListView1);
		annuaireAdapter = new ListAnnuaireAdapter(result, type);
		expandableListView.setAdapter(annuaireAdapter);

		expandableListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Annuaires annuaires = (Annuaires) annuaireAdapter.getItem(arg2);
				if (annuaires != null) {
					showDetail(annuaires);
					flipper.showNext();
				}
			}
		});
	}

	private void showDetail(Annuaires annuaires) {
		final ImageView centreImage = (ImageView) findViewById(R.id.centre_image);

		Callback callback = new Callback() {

			@Override
			public boolean handleMessage(Message msg) {
				if (msg.obj != null) {
					Bitmap bitmap = (Bitmap) msg.obj;
					centreImage.setImageBitmap(bitmap);
				} else {
					centreImage.setImageResource(R.drawable.default_image);

				}
				return false;
			}
		};
		title.setText(annuaires.getTitle());
		LoadImagePool.loadImage(this, annuaires.getImageURL(), new Handler(
				callback), true);
		TextView centreName = (TextView) findViewById(R.id.centre_name);
		centreName.setText(annuaires.getTitle());
		TextView centreTitle = (TextView) findViewById(R.id.centre_title);
		centreTitle.setText(annuaires.getActivity());

		TextView text = (TextView) findViewById(R.id.text_text);
		text.setText(annuaires.getText());

		TextView website = (TextView) findViewById(R.id.text_website);
		website.setText(annuaires.getWeb());
		Linkify.addLinks(website, Linkify.WEB_URLS);

		TextView centreResponsible = (TextView) findViewById(R.id.text_responsible);
		centreResponsible.setText(annuaires.getResponsible());

		TextView centre = (TextView) findViewById(R.id.text_centre);
		centre.setText(annuaires.getCentre());

	}
}
